package com.zzy.controller;

import com.zzy.dao.ProductModelDao;
import com.zzy.model.AjaxMsg;
import com.zzy.model.ProductModel;
import lombok.val;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.IndexColorModel;
import java.util.Iterator;
import java.util.List;

import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
import org.springframework.data.elasticsearch.core.query.Query;

@RestController
@RequestMapping(value = "/el")
public class ElasticserachController {

    @Resource
    @Qualifier("elasticsearchRestTemplate")
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @RequestMapping()
    public AjaxMsg index(){
        return AjaxMsg.returnAjaxMsg("el/index");
    }

    /**
     * 删除索引
     * @return
     */
    @RequestMapping("/delete/index") //http://localhost:8082/el/delete/index
    public AjaxMsg createIndex(){
        IndexCoordinates of = IndexCoordinates.of("zzyproduct");
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(of);
        boolean res =  indexOperations.delete();//新版的删除索引
        // String res = elasticsearchRestTemplate.deleteIndex(ProductModel.class);
        return AjaxMsg.returnAjaxMsg( res );//{ code: 200,  model: true, msg: "success" }
    }

    //这里为了快速测试，不建 service 了



    @Autowired
    ProductModelDao productModelDao;

    /**
     * 添加文档
     * @return
     */
    @RequestMapping("/save") //http://localhost:8082/el/save
    public AjaxMsg save(){
        for(int i=1;i<=15;i++){
            ProductModel model = new ProductModel();
            model.setId((long) i);
            model.setCategory("分类"+i);
            model.setTitle("标题"+i);
            model.setPrice(1.00 + i);
            model.setImages("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF");
             productModelDao.save(model);
        }
        return AjaxMsg.returnAjaxMsg( "success" );
    }



    /**
     * 修改 文档
     * @return
     */
    @RequestMapping("/update") //http://localhost:8082/el/update
    public AjaxMsg update(){
        ProductModel model = new ProductModel();
        model.setId(1L);
        model.setCategory("分类1-1");
        model.setTitle("标题1-1");
        model.setPrice(11.00);
        model.setImages("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF");
        Object o = productModelDao.save(model);
        return AjaxMsg.returnAjaxMsg( o );
    }


    /**
     * 根据ID查询
     * @return
     */
    @RequestMapping("/find/by/id") //http://localhost:8082/el/find/by/id?id=1
    public AjaxMsg findById(Long id){
        Object o = productModelDao.findById(id);
        return AjaxMsg.returnAjaxMsg( o );
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/delete") //http://localhost:8082/el/delete?id=2
    public AjaxMsg delete(Long id){
        ProductModel model = new ProductModel();
        model.setId(id);
        productModelDao.delete(model);
        return AjaxMsg.returnAjaxMsg( "success" );
    }



    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/find/all") //http://localhost:8082/el/find/all
    public AjaxMsg findAll(){
        Object o = productModelDao.findAll();
        return AjaxMsg.returnAjaxMsg( o );
    }



    /**
     *  复合查询
     * @return
     */
    @RequestMapping("/find")
    public AjaxMsg find(ProductModel model){
        Object o = "";
        Sort sort = Sort.by(Sort.Direction.DESC,"id");

        PageRequest request = PageRequest.of( (model.getPageNum() -1 ), model.getPageSize(),sort);



        Page<ProductModel> models = productModelDao.findAll(request);



        return AjaxMsg.returnAjaxMsg( models );
    }


    /**
     *  复合查询 2
     * @return
     */
    @RequestMapping("/find2")
    public AjaxMsg find2(ProductModel model){
        Object o = "";

        Sort sort = Sort.by(Sort.Direction.DESC,"id");

        Pageable page =  PageRequest.of( (model.getPageNum() -1 ), model.getPageSize(),sort);

        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        NativeSearchQuery query = nativeSearchQueryBuilder
                .withQuery(QueryBuilders.termQuery("category","分类6"))
                .withPageable(page)
                .build();

        SearchHits<ProductModel> search = elasticsearchRestTemplate.search(query, ProductModel.class);

        Iterator iterator = search.stream().iterator();
        while (iterator.hasNext()){
            Object o1 = iterator.next();
            System.out.println(o1);
        }


        return AjaxMsg.returnAjaxMsg( search );
    }





}
